--Generated Upgrade For Version 1.0.0.0.12
--Generated on 2014-09-27 13:10:20

--CREATE TABLE [ConfigSetting]
if not exists(select * from sysobjects where name = 'ConfigSetting' and xtype = 'U')
CREATE TABLE [dbo].[ConfigSetting] (
	[ID] [Int] IDENTITY (1, 1) NOT NULL ,
	[Name] [VarChar] (50) NOT NULL ,
	[Value] [VarChar] (100) NOT NULL ,
	[ModifiedBy] [Varchar] (50) NULL,
	[ModifiedDate] [DateTime] CONSTRAINT [DF__CONFIGSETTING_MODIFIEDDATE] DEFAULT getdate() NULL,
	[CreatedBy] [Varchar] (50) NULL,
	[CreatedDate] [DateTime] CONSTRAINT [DF__CONFIGSETTING_CREATEDDATE] DEFAULT getdate() NULL,
	[TimeStamp] [timestamp] NOT NULL
)

GO
--PRIMARY KEY FOR TABLE [ConfigSetting]
if not exists(select * from sysobjects where name = 'PK_CONFIGSETTING' and xtype = 'PK')
ALTER TABLE [dbo].[ConfigSetting] WITH NOCHECK ADD 
CONSTRAINT [PK_CONFIGSETTING] PRIMARY KEY CLUSTERED
(
	[ID]
)

GO
--REMOVE FOREIGN KEY
if exists(select * from sysobjects where name = 'FK__LISTING_BODYTYPE' and xtype = 'F')
ALTER TABLE [dbo].[Listing] DROP CONSTRAINT [FK__LISTING_BODYTYPE]
GO

--REMOVE FOREIGN KEY
if exists(select * from sysobjects where name = 'FK__CITYZIP_CITY' and xtype = 'F')
ALTER TABLE [dbo].[CityZip] DROP CONSTRAINT [FK__CITYZIP_CITY]
GO

--REMOVE FOREIGN KEY
if exists(select * from sysobjects where name = 'FK_EXTERIOR_LISTING_COLOR' and xtype = 'F')
ALTER TABLE [dbo].[Listing] DROP CONSTRAINT [FK_EXTERIOR_LISTING_COLOR]
GO

--REMOVE FOREIGN KEY
if exists(select * from sysobjects where name = 'FK_INTERIOR_LISTING_COLOR' and xtype = 'F')
ALTER TABLE [dbo].[Listing] DROP CONSTRAINT [FK_INTERIOR_LISTING_COLOR]
GO

--REMOVE FOREIGN KEY
if exists(select * from sysobjects where name = 'FK__LISTING_CONDITION' and xtype = 'F')
ALTER TABLE [dbo].[Listing] DROP CONSTRAINT [FK__LISTING_CONDITION]
GO

--REMOVE FOREIGN KEY
if exists(select * from sysobjects where name = 'FK__LISTINGFEATURE_FEATURE' and xtype = 'F')
ALTER TABLE [dbo].[ListingFeature] DROP CONSTRAINT [FK__LISTINGFEATURE_FEATURE]
GO

--REMOVE FOREIGN KEY
if exists(select * from sysobjects where name = 'FK__LISTING_FUELTYPE' and xtype = 'F')
ALTER TABLE [dbo].[Listing] DROP CONSTRAINT [FK__LISTING_FUELTYPE]
GO

--REMOVE FOREIGN KEY
if exists(select * from sysobjects where name = 'FK__LISTINGFEATURE_LISTING' and xtype = 'F')
ALTER TABLE [dbo].[ListingFeature] DROP CONSTRAINT [FK__LISTINGFEATURE_LISTING]
GO

--REMOVE FOREIGN KEY
if exists(select * from sysobjects where name = 'FK__BULLETPOINT_LISTING' and xtype = 'F')
ALTER TABLE [dbo].[BulletPoint] DROP CONSTRAINT [FK__BULLETPOINT_LISTING]
GO

--REMOVE FOREIGN KEY
if exists(select * from sysobjects where name = 'FK__LISTINGVIEW_LISTING' and xtype = 'F')
ALTER TABLE [dbo].[ListingView] DROP CONSTRAINT [FK__LISTINGVIEW_LISTING]
GO

--REMOVE FOREIGN KEY
if exists(select * from sysobjects where name = 'FK__LISTINGIMAGE_LISTING' and xtype = 'F')
ALTER TABLE [dbo].[ListingImage] DROP CONSTRAINT [FK__LISTINGIMAGE_LISTING]
GO

--REMOVE FOREIGN KEY
if exists(select * from sysobjects where name = 'FK__LISTINGPREMIUM_LISTING' and xtype = 'F')
ALTER TABLE [dbo].[ListingPremium] DROP CONSTRAINT [FK__LISTINGPREMIUM_LISTING]
GO

--REMOVE FOREIGN KEY
if exists(select * from sysobjects where name = 'FK__LISTINGSAVED_LISTING' and xtype = 'F')
ALTER TABLE [dbo].[ListingSaved] DROP CONSTRAINT [FK__LISTINGSAVED_LISTING]
GO

--REMOVE FOREIGN KEY
if exists(select * from sysobjects where name = 'FK__MAKEMODEL_MAKE' and xtype = 'F')
ALTER TABLE [dbo].[MakeModel] DROP CONSTRAINT [FK__MAKEMODEL_MAKE]
GO

--REMOVE FOREIGN KEY
if exists(select * from sysobjects where name = 'FK__LISTING_MAKEMODEL' and xtype = 'F')
ALTER TABLE [dbo].[Listing] DROP CONSTRAINT [FK__LISTING_MAKEMODEL]
GO

--REMOVE FOREIGN KEY
if exists(select * from sysobjects where name = 'FK__LISTING_METRO' and xtype = 'F')
ALTER TABLE [dbo].[Listing] DROP CONSTRAINT [FK__LISTING_METRO]
GO

--REMOVE FOREIGN KEY
if exists(select * from sysobjects where name = 'FK__DEALER_METRO' and xtype = 'F')
ALTER TABLE [dbo].[Dealer] DROP CONSTRAINT [FK__DEALER_METRO]
GO

--REMOVE FOREIGN KEY
if exists(select * from sysobjects where name = 'FK__METROSTATE_METRO' and xtype = 'F')
ALTER TABLE [dbo].[MetroState] DROP CONSTRAINT [FK__METROSTATE_METRO]
GO

--REMOVE FOREIGN KEY
if exists(select * from sysobjects where name = 'FK__METROZIP_METRO' and xtype = 'F')
ALTER TABLE [dbo].[MetroZip] DROP CONSTRAINT [FK__METROZIP_METRO]
GO

--REMOVE FOREIGN KEY
if exists(select * from sysobjects where name = 'FK__MAKEMODEL_MODEL' and xtype = 'F')
ALTER TABLE [dbo].[MakeModel] DROP CONSTRAINT [FK__MAKEMODEL_MODEL]
GO

--REMOVE FOREIGN KEY
if exists(select * from sysobjects where name = 'FK__SEARCHSAVEDCRITERIA_SEARCHSAVED' and xtype = 'F')
ALTER TABLE [dbo].[SearchSavedCriteria] DROP CONSTRAINT [FK__SEARCHSAVEDCRITERIA_SEARCHSAVED]
GO

--REMOVE FOREIGN KEY
if exists(select * from sysobjects where name = 'FK__CITY_STATE' and xtype = 'F')
ALTER TABLE [dbo].[City] DROP CONSTRAINT [FK__CITY_STATE]
GO

--REMOVE FOREIGN KEY
if exists(select * from sysobjects where name = 'FK__METROSTATE_STATE' and xtype = 'F')
ALTER TABLE [dbo].[MetroState] DROP CONSTRAINT [FK__METROSTATE_STATE]
GO

--REMOVE FOREIGN KEY
if exists(select * from sysobjects where name = 'FK__ZIP_STATE' and xtype = 'F')
ALTER TABLE [dbo].[Zip] DROP CONSTRAINT [FK__ZIP_STATE]
GO

--REMOVE FOREIGN KEY
if exists(select * from sysobjects where name = 'FK__USERACCOUNT_STATE' and xtype = 'F')
ALTER TABLE [dbo].[UserAccount] DROP CONSTRAINT [FK__USERACCOUNT_STATE]
GO

--REMOVE FOREIGN KEY
if exists(select * from sysobjects where name = 'FK__LISTING_TRANSMISSION' and xtype = 'F')
ALTER TABLE [dbo].[Listing] DROP CONSTRAINT [FK__LISTING_TRANSMISSION]
GO

--REMOVE FOREIGN KEY
if exists(select * from sysobjects where name = 'FK__USERLOGINHISTORY_USERACCOUNT' and xtype = 'F')
ALTER TABLE [dbo].[UserLoginHistory] DROP CONSTRAINT [FK__USERLOGINHISTORY_USERACCOUNT]
GO

--REMOVE FOREIGN KEY
if exists(select * from sysobjects where name = 'FK__LISTING_USERACCOUNT' and xtype = 'F')
ALTER TABLE [dbo].[Listing] DROP CONSTRAINT [FK__LISTING_USERACCOUNT]
GO

--REMOVE FOREIGN KEY
if exists(select * from sysobjects where name = 'FK__SEARCHSAVED_USERACCOUNT' and xtype = 'F')
ALTER TABLE [dbo].[SearchSaved] DROP CONSTRAINT [FK__SEARCHSAVED_USERACCOUNT]
GO

--REMOVE FOREIGN KEY
if exists(select * from sysobjects where name = 'FK__VISTORCONTACT_USERACCOUNT' and xtype = 'F')
ALTER TABLE [dbo].[VistorContact] DROP CONSTRAINT [FK__VISTORCONTACT_USERACCOUNT]
GO

--REMOVE FOREIGN KEY
if exists(select * from sysobjects where name = 'FK__LISTINGSAVED_USERACCOUNT' and xtype = 'F')
ALTER TABLE [dbo].[ListingSaved] DROP CONSTRAINT [FK__LISTINGSAVED_USERACCOUNT]
GO

--REMOVE FOREIGN KEY
if exists(select * from sysobjects where name = 'FK__USERACCOUNT_USERTYPE' and xtype = 'F')
ALTER TABLE [dbo].[UserAccount] DROP CONSTRAINT [FK__USERACCOUNT_USERTYPE]
GO

--REMOVE FOREIGN KEY
if exists(select * from sysobjects where name = 'FK__CITYZIP_ZIP' and xtype = 'F')
ALTER TABLE [dbo].[CityZip] DROP CONSTRAINT [FK__CITYZIP_ZIP]
GO

--REMOVE FOREIGN KEY
if exists(select * from sysobjects where name = 'FK__METROZIP_ZIP' and xtype = 'F')
ALTER TABLE [dbo].[MetroZip] DROP CONSTRAINT [FK__METROZIP_ZIP]
GO

--REMOVE FOREIGN KEY
if exists(select * from sysobjects where name = 'FK__USERACCOUNT_ZIP' and xtype = 'F')
ALTER TABLE [dbo].[UserAccount] DROP CONSTRAINT [FK__USERACCOUNT_ZIP]
GO

--FOREIGN KEY RELATIONSHIP [BodyType] -> [Listing] ([BodyType].[BodyTypeId] -> [Listing].[BodyTypeId])
if not exists(select * from sysobjects where name = 'FK__LISTING_BODYTYPE' and xtype = 'F')
ALTER TABLE [dbo].[Listing] ADD 
CONSTRAINT [FK__LISTING_BODYTYPE] FOREIGN KEY 
(
	[BodyTypeId]
) REFERENCES [dbo].[BodyType] (
	[BodyTypeId]
)
GO

--FOREIGN KEY RELATIONSHIP [City] -> [CityZip] ([City].[CityId] -> [CityZip].[CityId])
if not exists(select * from sysobjects where name = 'FK__CITYZIP_CITY' and xtype = 'F')
ALTER TABLE [dbo].[CityZip] ADD 
CONSTRAINT [FK__CITYZIP_CITY] FOREIGN KEY 
(
	[CityId]
) REFERENCES [dbo].[City] (
	[CityId]
)
GO

--FOREIGN KEY RELATIONSHIP [Color] -> [Listing] ([Color].[ColorId] -> [Listing].[ColorExtId])
if not exists(select * from sysobjects where name = 'FK_EXTERIOR_LISTING_COLOR' and xtype = 'F')
ALTER TABLE [dbo].[Listing] ADD 
CONSTRAINT [FK_EXTERIOR_LISTING_COLOR] FOREIGN KEY 
(
	[ColorExtId]
) REFERENCES [dbo].[Color] (
	[ColorId]
)
GO

--FOREIGN KEY RELATIONSHIP [Color] -> [Listing] ([Color].[ColorId] -> [Listing].[ColorIntId])
if not exists(select * from sysobjects where name = 'FK_INTERIOR_LISTING_COLOR' and xtype = 'F')
ALTER TABLE [dbo].[Listing] ADD 
CONSTRAINT [FK_INTERIOR_LISTING_COLOR] FOREIGN KEY 
(
	[ColorIntId]
) REFERENCES [dbo].[Color] (
	[ColorId]
)
GO

--FOREIGN KEY RELATIONSHIP [Condition] -> [Listing] ([Condition].[ConditionId] -> [Listing].[ConditionId])
if not exists(select * from sysobjects where name = 'FK__LISTING_CONDITION' and xtype = 'F')
ALTER TABLE [dbo].[Listing] ADD 
CONSTRAINT [FK__LISTING_CONDITION] FOREIGN KEY 
(
	[ConditionId]
) REFERENCES [dbo].[Condition] (
	[ConditionId]
)
GO

--FOREIGN KEY RELATIONSHIP [Feature] -> [ListingFeature] ([Feature].[FeatureId] -> [ListingFeature].[FeatureId])
if not exists(select * from sysobjects where name = 'FK__LISTINGFEATURE_FEATURE' and xtype = 'F')
ALTER TABLE [dbo].[ListingFeature] ADD 
CONSTRAINT [FK__LISTINGFEATURE_FEATURE] FOREIGN KEY 
(
	[FeatureId]
) REFERENCES [dbo].[Feature] (
	[FeatureId]
)
GO

--FOREIGN KEY RELATIONSHIP [FuelType] -> [Listing] ([FuelType].[FuelTypeId] -> [Listing].[FuelTypeId])
if not exists(select * from sysobjects where name = 'FK__LISTING_FUELTYPE' and xtype = 'F')
ALTER TABLE [dbo].[Listing] ADD 
CONSTRAINT [FK__LISTING_FUELTYPE] FOREIGN KEY 
(
	[FuelTypeId]
) REFERENCES [dbo].[FuelType] (
	[FuelTypeId]
)
GO

--FOREIGN KEY RELATIONSHIP [Listing] -> [ListingFeature] ([Listing].[ListingId] -> [ListingFeature].[ListingId])
if not exists(select * from sysobjects where name = 'FK__LISTINGFEATURE_LISTING' and xtype = 'F')
ALTER TABLE [dbo].[ListingFeature] ADD 
CONSTRAINT [FK__LISTINGFEATURE_LISTING] FOREIGN KEY 
(
	[ListingId]
) REFERENCES [dbo].[Listing] (
	[ListingId]
)
GO

--FOREIGN KEY RELATIONSHIP [Listing] -> [BulletPoint] ([Listing].[ListingId] -> [BulletPoint].[ListingId])
if not exists(select * from sysobjects where name = 'FK__BULLETPOINT_LISTING' and xtype = 'F')
ALTER TABLE [dbo].[BulletPoint] ADD 
CONSTRAINT [FK__BULLETPOINT_LISTING] FOREIGN KEY 
(
	[ListingId]
) REFERENCES [dbo].[Listing] (
	[ListingId]
)
GO

--FOREIGN KEY RELATIONSHIP [Listing] -> [ListingView] ([Listing].[ListingId] -> [ListingView].[ListingId])
if not exists(select * from sysobjects where name = 'FK__LISTINGVIEW_LISTING' and xtype = 'F')
ALTER TABLE [dbo].[ListingView] ADD 
CONSTRAINT [FK__LISTINGVIEW_LISTING] FOREIGN KEY 
(
	[ListingId]
) REFERENCES [dbo].[Listing] (
	[ListingId]
)
GO

--FOREIGN KEY RELATIONSHIP [Listing] -> [ListingImage] ([Listing].[ListingId] -> [ListingImage].[ListingId])
if not exists(select * from sysobjects where name = 'FK__LISTINGIMAGE_LISTING' and xtype = 'F')
ALTER TABLE [dbo].[ListingImage] ADD 
CONSTRAINT [FK__LISTINGIMAGE_LISTING] FOREIGN KEY 
(
	[ListingId]
) REFERENCES [dbo].[Listing] (
	[ListingId]
)
GO

--FOREIGN KEY RELATIONSHIP [Listing] -> [ListingPremium] ([Listing].[ListingId] -> [ListingPremium].[ListingId])
if not exists(select * from sysobjects where name = 'FK__LISTINGPREMIUM_LISTING' and xtype = 'F')
ALTER TABLE [dbo].[ListingPremium] ADD 
CONSTRAINT [FK__LISTINGPREMIUM_LISTING] FOREIGN KEY 
(
	[ListingId]
) REFERENCES [dbo].[Listing] (
	[ListingId]
)
GO

--FOREIGN KEY RELATIONSHIP [Listing] -> [ListingSaved] ([Listing].[ListingId] -> [ListingSaved].[ListingId])
if not exists(select * from sysobjects where name = 'FK__LISTINGSAVED_LISTING' and xtype = 'F')
ALTER TABLE [dbo].[ListingSaved] ADD 
CONSTRAINT [FK__LISTINGSAVED_LISTING] FOREIGN KEY 
(
	[ListingId]
) REFERENCES [dbo].[Listing] (
	[ListingId]
)
GO

--FOREIGN KEY RELATIONSHIP [Make] -> [MakeModel] ([Make].[MakeId] -> [MakeModel].[MakeId])
if not exists(select * from sysobjects where name = 'FK__MAKEMODEL_MAKE' and xtype = 'F')
ALTER TABLE [dbo].[MakeModel] ADD 
CONSTRAINT [FK__MAKEMODEL_MAKE] FOREIGN KEY 
(
	[MakeId]
) REFERENCES [dbo].[Make] (
	[MakeId]
)
GO

--FOREIGN KEY RELATIONSHIP [MakeModel] -> [Listing] ([MakeModel].[MakeModelId] -> [Listing].[MakeModelId])
if not exists(select * from sysobjects where name = 'FK__LISTING_MAKEMODEL' and xtype = 'F')
ALTER TABLE [dbo].[Listing] ADD 
CONSTRAINT [FK__LISTING_MAKEMODEL] FOREIGN KEY 
(
	[MakeModelId]
) REFERENCES [dbo].[MakeModel] (
	[MakeModelId]
)
GO

--FOREIGN KEY RELATIONSHIP [Metro] -> [Listing] ([Metro].[MetroId] -> [Listing].[MetroId])
if not exists(select * from sysobjects where name = 'FK__LISTING_METRO' and xtype = 'F')
ALTER TABLE [dbo].[Listing] ADD 
CONSTRAINT [FK__LISTING_METRO] FOREIGN KEY 
(
	[MetroId]
) REFERENCES [dbo].[Metro] (
	[MetroId]
)
GO

--FOREIGN KEY RELATIONSHIP [Metro] -> [Dealer] ([Metro].[MetroId] -> [Dealer].[MetroId])
if not exists(select * from sysobjects where name = 'FK__DEALER_METRO' and xtype = 'F')
ALTER TABLE [dbo].[Dealer] ADD 
CONSTRAINT [FK__DEALER_METRO] FOREIGN KEY 
(
	[MetroId]
) REFERENCES [dbo].[Metro] (
	[MetroId]
)
GO

--FOREIGN KEY RELATIONSHIP [Metro] -> [MetroState] ([Metro].[MetroId] -> [MetroState].[MetroId])
if not exists(select * from sysobjects where name = 'FK__METROSTATE_METRO' and xtype = 'F')
ALTER TABLE [dbo].[MetroState] ADD 
CONSTRAINT [FK__METROSTATE_METRO] FOREIGN KEY 
(
	[MetroId]
) REFERENCES [dbo].[Metro] (
	[MetroId]
)
GO

--FOREIGN KEY RELATIONSHIP [Metro] -> [MetroZip] ([Metro].[MetroId] -> [MetroZip].[MetroId])
if not exists(select * from sysobjects where name = 'FK__METROZIP_METRO' and xtype = 'F')
ALTER TABLE [dbo].[MetroZip] ADD 
CONSTRAINT [FK__METROZIP_METRO] FOREIGN KEY 
(
	[MetroId]
) REFERENCES [dbo].[Metro] (
	[MetroId]
)
GO

--FOREIGN KEY RELATIONSHIP [Model] -> [MakeModel] ([Model].[ModelId] -> [MakeModel].[ModelId])
if not exists(select * from sysobjects where name = 'FK__MAKEMODEL_MODEL' and xtype = 'F')
ALTER TABLE [dbo].[MakeModel] ADD 
CONSTRAINT [FK__MAKEMODEL_MODEL] FOREIGN KEY 
(
	[ModelId]
) REFERENCES [dbo].[Model] (
	[ModelId]
)
GO

--FOREIGN KEY RELATIONSHIP [SearchSaved] -> [SearchSavedCriteria] ([SearchSaved].[SearchSavedId] -> [SearchSavedCriteria].[SearchSavedId])
if not exists(select * from sysobjects where name = 'FK__SEARCHSAVEDCRITERIA_SEARCHSAVED' and xtype = 'F')
ALTER TABLE [dbo].[SearchSavedCriteria] ADD 
CONSTRAINT [FK__SEARCHSAVEDCRITERIA_SEARCHSAVED] FOREIGN KEY 
(
	[SearchSavedId]
) REFERENCES [dbo].[SearchSaved] (
	[SearchSavedId]
)
GO

--FOREIGN KEY RELATIONSHIP [State] -> [City] ([State].[StateId] -> [City].[StateId])
if not exists(select * from sysobjects where name = 'FK__CITY_STATE' and xtype = 'F')
ALTER TABLE [dbo].[City] ADD 
CONSTRAINT [FK__CITY_STATE] FOREIGN KEY 
(
	[StateId]
) REFERENCES [dbo].[State] (
	[StateId]
)
GO

--FOREIGN KEY RELATIONSHIP [State] -> [MetroState] ([State].[StateId] -> [MetroState].[StateId])
if not exists(select * from sysobjects where name = 'FK__METROSTATE_STATE' and xtype = 'F')
ALTER TABLE [dbo].[MetroState] ADD 
CONSTRAINT [FK__METROSTATE_STATE] FOREIGN KEY 
(
	[StateId]
) REFERENCES [dbo].[State] (
	[StateId]
)
GO

--FOREIGN KEY RELATIONSHIP [State] -> [Zip] ([State].[StateId] -> [Zip].[StateId])
if not exists(select * from sysobjects where name = 'FK__ZIP_STATE' and xtype = 'F')
ALTER TABLE [dbo].[Zip] ADD 
CONSTRAINT [FK__ZIP_STATE] FOREIGN KEY 
(
	[StateId]
) REFERENCES [dbo].[State] (
	[StateId]
)
GO

--FOREIGN KEY RELATIONSHIP [State] -> [UserAccount] ([State].[StateId] -> [UserAccount].[StateId])
if not exists(select * from sysobjects where name = 'FK__USERACCOUNT_STATE' and xtype = 'F')
ALTER TABLE [dbo].[UserAccount] ADD 
CONSTRAINT [FK__USERACCOUNT_STATE] FOREIGN KEY 
(
	[StateId]
) REFERENCES [dbo].[State] (
	[StateId]
)
GO

--FOREIGN KEY RELATIONSHIP [Transmission] -> [Listing] ([Transmission].[TransmissionId] -> [Listing].[TransmissionId])
if not exists(select * from sysobjects where name = 'FK__LISTING_TRANSMISSION' and xtype = 'F')
ALTER TABLE [dbo].[Listing] ADD 
CONSTRAINT [FK__LISTING_TRANSMISSION] FOREIGN KEY 
(
	[TransmissionId]
) REFERENCES [dbo].[Transmission] (
	[TransmissionId]
)
GO

--FOREIGN KEY RELATIONSHIP [UserAccount] -> [UserLoginHistory] ([UserAccount].[UserId] -> [UserLoginHistory].[UserId])
if not exists(select * from sysobjects where name = 'FK__USERLOGINHISTORY_USERACCOUNT' and xtype = 'F')
ALTER TABLE [dbo].[UserLoginHistory] ADD 
CONSTRAINT [FK__USERLOGINHISTORY_USERACCOUNT] FOREIGN KEY 
(
	[UserId]
) REFERENCES [dbo].[UserAccount] (
	[UserId]
)
GO

--FOREIGN KEY RELATIONSHIP [UserAccount] -> [Listing] ([UserAccount].[UserId] -> [Listing].[UserId])
if not exists(select * from sysobjects where name = 'FK__LISTING_USERACCOUNT' and xtype = 'F')
ALTER TABLE [dbo].[Listing] ADD 
CONSTRAINT [FK__LISTING_USERACCOUNT] FOREIGN KEY 
(
	[UserId]
) REFERENCES [dbo].[UserAccount] (
	[UserId]
)
GO

--FOREIGN KEY RELATIONSHIP [UserAccount] -> [SearchSaved] ([UserAccount].[UserId] -> [SearchSaved].[UserId])
if not exists(select * from sysobjects where name = 'FK__SEARCHSAVED_USERACCOUNT' and xtype = 'F')
ALTER TABLE [dbo].[SearchSaved] ADD 
CONSTRAINT [FK__SEARCHSAVED_USERACCOUNT] FOREIGN KEY 
(
	[UserId]
) REFERENCES [dbo].[UserAccount] (
	[UserId]
)
GO

--FOREIGN KEY RELATIONSHIP [UserAccount] -> [VistorContact] ([UserAccount].[UserId] -> [VistorContact].[UserId])
if not exists(select * from sysobjects where name = 'FK__VISTORCONTACT_USERACCOUNT' and xtype = 'F')
ALTER TABLE [dbo].[VistorContact] ADD 
CONSTRAINT [FK__VISTORCONTACT_USERACCOUNT] FOREIGN KEY 
(
	[UserId]
) REFERENCES [dbo].[UserAccount] (
	[UserId]
)
GO

--FOREIGN KEY RELATIONSHIP [UserAccount] -> [ListingSaved] ([UserAccount].[UserId] -> [ListingSaved].[UserId])
if not exists(select * from sysobjects where name = 'FK__LISTINGSAVED_USERACCOUNT' and xtype = 'F')
ALTER TABLE [dbo].[ListingSaved] ADD 
CONSTRAINT [FK__LISTINGSAVED_USERACCOUNT] FOREIGN KEY 
(
	[UserId]
) REFERENCES [dbo].[UserAccount] (
	[UserId]
)
GO

--FOREIGN KEY RELATIONSHIP [UserType] -> [UserAccount] ([UserType].[UserTypeId] -> [UserAccount].[UserTypeId])
if not exists(select * from sysobjects where name = 'FK__USERACCOUNT_USERTYPE' and xtype = 'F')
ALTER TABLE [dbo].[UserAccount] ADD 
CONSTRAINT [FK__USERACCOUNT_USERTYPE] FOREIGN KEY 
(
	[UserTypeId]
) REFERENCES [dbo].[UserType] (
	[UserTypeId]
)
GO

--FOREIGN KEY RELATIONSHIP [Zip] -> [CityZip] ([Zip].[ZipId] -> [CityZip].[ZipId])
if not exists(select * from sysobjects where name = 'FK__CITYZIP_ZIP' and xtype = 'F')
ALTER TABLE [dbo].[CityZip] ADD 
CONSTRAINT [FK__CITYZIP_ZIP] FOREIGN KEY 
(
	[ZipId]
) REFERENCES [dbo].[Zip] (
	[ZipId]
)
GO

--FOREIGN KEY RELATIONSHIP [Zip] -> [MetroZip] ([Zip].[ZipId] -> [MetroZip].[ZipId])
if not exists(select * from sysobjects where name = 'FK__METROZIP_ZIP' and xtype = 'F')
ALTER TABLE [dbo].[MetroZip] ADD 
CONSTRAINT [FK__METROZIP_ZIP] FOREIGN KEY 
(
	[ZipId]
) REFERENCES [dbo].[Zip] (
	[ZipId]
)
GO

--FOREIGN KEY RELATIONSHIP [Zip] -> [UserAccount] ([Zip].[ZipId] -> [UserAccount].[ZipId])
if not exists(select * from sysobjects where name = 'FK__USERACCOUNT_ZIP' and xtype = 'F')
ALTER TABLE [dbo].[UserAccount] ADD 
CONSTRAINT [FK__USERACCOUNT_ZIP] FOREIGN KEY 
(
	[ZipId]
) REFERENCES [dbo].[Zip] (
	[ZipId]
)
GO

